package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.view.fragment.BigImageFragment;

import org.bining.footstone.utils.StatusBarUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 详情页查看图片的activity
 */
public class ImagePagerActivity extends ActionbarActivity implements ViewPager.OnPageChangeListener {

    public static final String IMG_URLS = "mImageUrls";
    public static final String POSITION = "position";

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.vp_pics)
    ViewPager mVpPics;
    @BindView(R.id.tv_indicator)
    TextView mTvIndicator;
    @BindView(R.id.tv_save)
    TextView mTvSave;

    private List<String> mImageUrls = new ArrayList<String>();
    private List<BigImageFragment> mFragments = new ArrayList<BigImageFragment>();
    private int mCurrentPosition;
    private Map<Integer, Boolean> mDownloadingFlagMap = new HashMap<>();//用于保存对应位置图片是否在下载的标识

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_image_pager;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);

        StatusBarUtils.setTranslucent(this);

        mImageUrls = getIntent().getStringArrayListExtra(IMG_URLS);
        int position = getIntent().getIntExtra(POSITION, 0);
        mCurrentPosition = position;

        for (int i = 0; i < mImageUrls.size(); i++) {
            String url = mImageUrls.get(i);
            mFragments.add(BigImageFragment.newInstance(url));//添加到fragment集合中
            mDownloadingFlagMap.put(i, false);//初始化map，一开始全部的值都为false
        }

        mVpPics.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mVpPics.addOnPageChangeListener(this);

        mVpPics.setCurrentItem(mCurrentPosition);// 设置当前所在的位置
        setIndicator(mCurrentPosition);//设置当前位置指示

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mCurrentPosition = position;
        // 页面变化时，设置当前的指示
        setIndicator(mCurrentPosition);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    private void setIndicator(int position) {
        mTvIndicator.setText(position + 1 + "/" + mImageUrls.size());//设置当前的指示
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
